/*
 * AllCustomerTablePanel.java
 *
 * Created on January 8, 2007, 6:10 AM
 */
package trader.gui;
import trader.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
/**
 *
 * @author  vp200359
 */
public class AllCustomerTablePanel extends javax.swing.JPanel implements BrokerPanel {
    BrokerModel model;
    BrokerController controller;
    String [] tableHeaders = {"Customer Id", "Name", "Address"};
    private javax.swing.table.DefaultTableModel tableModel;
    
    /** Creates new form AllCustomerTablePanel */
    public AllCustomerTablePanel() {
        initComponents();
    }
    
    public void refresh() {
        try {
            Customer[] customers = model.getAllCustomers();
            refreshAllCustPan(customers);
        } catch(Exception e) {
            if (model == null) {
            } else {
                System.out.println(e.toString());
            }
        }
    }
    
    public void registerController(BrokerController controller) {
        this.controller = controller;
    }
    
    public void registerModel(BrokerModel model) {
        this.model = model;
        tableModel = (DefaultTableModel) jTable1.getModel();
        refresh();  
    }
    
    public void display(Object obj){
        if(obj instanceof Customer)
            refresh();
    }
    
    public JPanel getPanel() {
        return this;
    }    
    public void refreshAllCustPan(Customer[] custs){
        // Hint This method is similar to the updateTable method
        // of TableExample class
        String newData[][];
        //** 1 Create a 2-dimensional string array with no of rows
        //**   equal to custs.length and no of columns set to 3, i
        //**   and assign to newData.

        //** 2 Write a for loop to populate the newData array with
        //**   customer id, name, and addr obtained from custs array

        //** 3 Invoke the setDataVector method on the tableModel
        //**   passing it newData and tableHeaders arrays.

    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("All Customers Table");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Customer Id", "Name", "Address"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 415, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 415, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
}